package gitee.derumin.lifeissohard.tools;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;

public abstract class LishNetworkPackets {

    public static PacketByteBuf createHungerManagerPacket(HungerManager hungerManager){
        PacketByteBuf buf = PacketByteBufs.create();
        NbtCompound nbt = new NbtCompound();
        hungerManager.writeNbt(nbt);
        buf.writeNbt(nbt);
        return buf;
    }

    public static void applyHungerManagerPacket(MinecraftClient client, PacketByteBuf buf){
        NbtCompound nbt = buf.readNbt();
        client.execute(() -> {
            if (client.player != null){
                client.player.getHungerManager().readNbt(nbt);
            }});
    }

    public static PacketByteBuf createPlayerMoveInputPacket(float movementForward,float movementSideways,boolean pressingForward,boolean pressingBack,boolean pressingLeft,boolean pressingRight,boolean jumping,boolean sneaking){
        PacketByteBuf buf = PacketByteBufs.create();
        buf.writeFloat(movementForward);
        buf.writeFloat(movementSideways);
        buf.writeBoolean(pressingForward);
        buf.writeBoolean(pressingBack);
        buf.writeBoolean(pressingLeft);
        buf.writeBoolean(pressingRight);
        buf.writeBoolean(jumping);
        buf.writeBoolean(sneaking);
        return buf;
    }

    public static void applyPlayerMoveInputPacket(MinecraftServer server, ServerPlayerEntity player, PacketByteBuf buf){
        buf.readFloat();
        buf.readFloat();
        boolean pressingForward = buf.readBoolean();
        boolean pressingBack = buf.readBoolean();
        boolean pressingLeft = buf.readBoolean();
        boolean pressingRight = buf.readBoolean();
        buf.readBoolean();
        buf.readBoolean();
        server.execute(() -> {
            if (player.getHungerManager() instanceof LishHungerManager lishHungerManager){
                lishHungerManager.setInputtingState(pressingForward||pressingBack||pressingLeft||pressingRight);
            }
        });
    }
}
